package com.cloud.alibaba.common.util;

import java.security.MessageDigest;

/**
 * Filename: Md5Util
 * Description:
 * Copyright: Copyright (c)2020
 * Company: tuliang
 *
 * @author: lihaojie
 * @version: 1.0 Create at: 2020-10-27 14:33
 * <p/>
 * Modification History: Date Author Version Description
 * ------------------------------------------------------------------
 * 2020-10-27 lihaojie 1.0 1.0 Version
 */
public class Md5Util {

    private static final String encryModel = "MD5";

    /**
     * 32λmd5.
     * 32位小写md5加密
     *
     * @param str
     * @return
     */
    public static String md5(String str) {
        return encrypt(encryModel, str);
    }


    public static String encrypt(String algorithm, String str) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            md.update(str.getBytes());

            byte[] bytes = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < bytes.length; i++) {
                int b = bytes[i] & 0xFF;
                if (b < 0x10) {
                    sb.append('0');
                }
                sb.append(Integer.toHexString(b));
            }
            return sb.toString();
        } catch (Exception e) {
            return "";
        }
    }
}
